package com.sneakxy.cloudbase.platform.controller;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.validation.BindingResult;

import com.sneakxy.cloudbase.platform.shiro.ShiroAccount;
import com.sneakxy.cloudbase.platform.utils.BindingResultUtils;
import com.sneakxy.cloudbase.platform.utils.web.Response;

/**
 * @author 潜行的青衣
 * @see
 */
public abstract class BaseController {

	protected final Logger log = LogManager.getLogger(this.getClass());
	
	public Map<String, String> getFieldErrors(BindingResult result) {
		return BindingResultUtils.getFieldErrors(result);
	}
	
	public Response<Map<String, String>> getFieldErrorsResponse(BindingResult result) {
		return BindingResultUtils.getFieldErrorsResponse(result);
	}
	
	public boolean hasRole(String roleIdentifier) {
		return SecurityUtils.getSubject().hasRole(roleIdentifier);
	}
	
	public boolean isPermitted(String permission) {
		return SecurityUtils.getSubject().isPermitted(permission);
	}
	
	public ShiroAccount getLoginAccount() {
		return (ShiroAccount) SecurityUtils.getSubject().getPrincipal();
	}
	
}
